package com.yiqi.liebang.feature.wallet.view;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.BanlanceBo;
import com.yiqi.liebang.entity.bo.PayBo;
import com.yiqi.liebang.entity.bo.PublicBo;
import com.yiqi.liebang.feature.wallet.contract.WalletContract;
import com.yiqi.liebang.feature.wallet.dependencies.wallet.DaggerWalletComponent;
import com.yiqi.liebang.feature.wallet.dependencies.wallet.WalletPresenterModule;
import com.yiqi.liebang.feature.wallet.view.adapter.WalletAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.suozhang.framework.utils.AppUtil.fullScreen;

public class WalletActivity extends BaseActivity implements WalletContract.View, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_details)
    RecyclerView mRvDetails;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    @BindView(R.id.tv_balance)
    TextView mTvBalance;
    private WalletAdapter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<BanlanceBo.ListData> mData;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_wallet;
    }

    @Inject
    WalletContract.Presenter mPresenter;

    @Override
    protected void initInjector() {
        DaggerWalletComponent.builder().walletPresenterModule(new WalletPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "我的钱包", true, true);
        initAdapter();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onStart() {
        super.onStart();
        pageIndex = 1;
        mPresenter.getUserAssetsDetail(pageIndex, pageSize);
    }

    @Override
    protected void initSystemParams() {
//        super.initSystemParams();
        fullScreen(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }


    @OnClick({R.id.btn_wallet_recharge, R.id.btn_wallet_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_wallet_recharge:
                startActivity(new Intent(this, RechargeActivity.class));
                break;
            case R.id.btn_wallet_withdraw:
                startActivity(new Intent(this, WithdrawActivity.class));
                break;
        }
    }

    @Override
    public void showSuccess(String error) {
        mTvBalance.setText(error);
    }

    @Override
    public void showPaySuccess(PayBo classifyBos) {

    }

    @Override
    public void userWithdrawSucc(PublicBo entity) {

    }

    @Override
    public void showTransactionListSuccess(List<BanlanceBo.ListData> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
        mSmartRefesh.finishRefresh();
    }

    @Override
    public void showTransactionListMoreSuccess(List<BanlanceBo.ListData> msg) {
        setData(false, msg);
    }

    @Override
    public void showTransactionListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showTransactionListEmpty() {
        mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView("暂无收支明细"));
        mAdapter.setNewData(null);
    }

    private void initAdapter() {

        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvDetails);
        mRvDetails.setLayoutManager(new LinearLayoutManager(this));
        mRvDetails.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new WalletAdapter();
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.bindToRecyclerView(mRvDetails);
        mAdapter.setOnLoadMoreListener(this, mRvDetails);
        mAdapter.setOnItemClickListener(this);


    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }


    private void refresh() {
        mAdapter.setEnableLoadMore(false);
        pageIndex = 1;
        mPresenter.getUserAssetsDetail(pageIndex, pageSize);
    }

    private void loadMore() {
        mPresenter.getUserAssetsDetailMore(pageIndex, pageSize);
    }


    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

}
